<template>
  <div class="config-manager">
    <div class="config-header">
      <h3>系统配置</h3>
      <button @click="toggleConfig" class="toggle-btn">
        {{ showConfig ? '隐藏配置' : '显示配置' }}
      </button>
    </div>
    
    <div v-if="showConfig" class="config-content">
      <div class="config-section">
        <h4>API配置</h4>
        <div class="config-item">
          <label>服务器地址:</label>
          <input 
            v-model="config.api.baseUrl" 
            type="text" 
            placeholder="http://192.168.0.119:91"
            class="config-input"
          />
        </div>
        <div class="config-item">
          <label>超时时间:</label>
          <input 
            v-model.number="config.api.timeout" 
            type="number" 
            min="1000"
            class="config-input"
          />
          <span class="unit">毫秒</span>
        </div>
      </div>
      
      <div class="config-actions">
        <button @click="saveConfig" class="save-btn">保存配置</button>
        <button @click="resetConfig" class="reset-btn">重置配置</button>
        <button @click="testConnection" class="test-btn">测试连接</button>
      </div>
      
      <div v-if="message" class="message" :class="messageType">
        {{ message }}
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { getConfig, loadConfig, saveConfig as saveConfigUtil, updateApiBaseUrl } from '../utils/config'

const showConfig = ref(false)
const message = ref('')
const messageType = ref('')

const config = reactive({
  api: {
    baseUrl: '',
    timeout: 10000
  }
})

// 加载当前配置
onMounted(async () => {
  await loadConfig()
  const currentConfig = getConfig()
  config.api.baseUrl = currentConfig.api.baseUrl
  config.api.timeout = currentConfig.api.timeout
})

const toggleConfig = () => {
  showConfig.value = !showConfig.value
}

const saveConfig = async () => {
  try {
    updateApiBaseUrl(config.api.baseUrl)
    saveConfigUtil({
      api: {
        baseUrl: config.api.baseUrl,
        timeout: config.api.timeout
      }
    })
    
    message.value = '配置保存成功！请重启应用使配置生效。'
    messageType.value = 'success'
    
    setTimeout(() => {
      message.value = ''
    }, 3000)
  } catch (error) {
    message.value = '配置保存失败：' + error
    messageType.value = 'error'
  }
}

const resetConfig = () => {
  config.api.baseUrl = 'http://192.168.0.119:91'
  config.api.timeout = 10000
  message.value = '配置已重置'
  messageType.value = 'info'
}

const testConnection = async () => {
  try {
    const controller = new AbortController()
    const timeoutId = setTimeout(() => controller.abort(), config.api.timeout)
    
    const response = await fetch(`${config.api.baseUrl}/health`, {
      method: 'GET',
      signal: controller.signal
    })
    
    clearTimeout(timeoutId)
    
    if (response.ok) {
      message.value = '连接测试成功！'
      messageType.value = 'success'
    } else {
      message.value = '连接测试失败：服务器响应异常'
      messageType.value = 'error'
    }
  } catch (error) {
    message.value = '连接测试失败：' + error
    messageType.value = 'error'
  }
  
  setTimeout(() => {
    message.value = ''
  }, 3000)
}
</script>

<style scoped>
.config-manager {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  padding: 20px;
  margin: 20px 0;
}

.config-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.config-header h3 {
  margin: 0;
  color: #fff;
}

.toggle-btn {
  background: #007bff;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}

.toggle-btn:hover {
  background: #0056b3;
}

.config-content {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  padding: 20px;
}

.config-section {
  margin-bottom: 20px;
}

.config-section h4 {
  color: #fff;
  margin-bottom: 15px;
}

.config-item {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.config-item label {
  color: #fff;
  width: 120px;
  margin-right: 10px;
}

.config-input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  background: #fff;
}

.unit {
  color: #ccc;
  margin-left: 10px;
}

.config-actions {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.save-btn, .reset-btn, .test-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: bold;
}

.save-btn {
  background: #28a745;
  color: white;
}

.reset-btn {
  background: #ffc107;
  color: #000;
}

.test-btn {
  background: #17a2b8;
  color: white;
}

.save-btn:hover {
  background: #218838;
}

.reset-btn:hover {
  background: #e0a800;
}

.test-btn:hover {
  background: #138496;
}

.message {
  padding: 10px;
  border-radius: 4px;
  font-weight: bold;
}

.message.success {
  background: #d4edda;
  color: #155724;
  border: 1px solid #c3e6cb;
}

.message.error {
  background: #f8d7da;
  color: #721c24;
  border: 1px solid #f5c6cb;
}

.message.info {
  background: #d1ecf1;
  color: #0c5460;
  border: 1px solid #bee5eb;
}
</style>
